package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.StoreProjectStage;
import com.dingreading.cloud.admin.entity.table.StoreProjectStageTableDef;
import com.dingreading.cloud.admin.mapper.StoreProjectStageMapper;
import com.dingreading.cloud.admin.service.StoreProjectStageService;
import com.dingreading.cloud.common.util.NepUtil;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-02-26
 */
@Service
public class StoreProjectStageServiceImpl extends BaseServiceImpl<StoreProjectStageMapper, StoreProjectStage, StoreProjectStageTableDef> implements StoreProjectStageService {

    @Override
    protected StoreProjectStageTableDef getTable() {
        return StoreProjectStageTableDef.storeProjectStage;
    }

    @Override
    public boolean haveSystemStageName(String storeUid, String uid, String projectUid, String stageName) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(uid))
            condition.and(table.uid.ne(uid));
        condition.and(table.stageName.eq(stageName))
                .and(table.projectUid.eq(projectUid));
        if (StringUtils.isBlank(storeUid))
            condition.and(table.source.eq(1));
        else
            condition.and(table.source.eq(2))
                    .and(table.storeUid.eq(storeUid));

        return exists(condition);
    }

    @Override
    public boolean updateEnabled(String storeUid, String uid, Integer enabled, String staffUid, String staffName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid));
        if (StringUtils.isBlank(storeUid))
            condition.and(table.source.eq(1));
        else
            condition.and(table.source.eq(2))
                    .and(table.storeUid.eq(storeUid));

        return UpdateChain.of(StoreProjectStage.class)
                .set(table.enabled, enabled)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

    @Override
    public int dropBean(String storeUid, String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid));
        if (StringUtils.isBlank(storeUid))
            condition.and(table.source.eq(1));
        else
            condition.and(table.source.eq(2))
                    .and(table.storeUid.eq(storeUid));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public List<StoreProjectStage> getByProjectUid(String projectUid, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.projectUid.eq(projectUid));
        if (null != enabled)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public int getMaxShowOrder(String storeUid, String projectUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isBlank(storeUid))
            condition.and(table.source.eq(1));
        else
            condition.and(table.source.eq(2))
                    .and(table.storeUid.eq(storeUid));
        condition.and(table.projectUid.eq(projectUid));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.orderNum))
                .from(table)
                .where(condition);

        Integer integer = getMapper().selectObjectByQueryAs(wrapper, Integer.class);
        return NepUtil.nullToZero(integer);
    }

    @Override
    public StoreProjectStage getGtOrderNum(String storeUid, String projectUid, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.projectUid.eq(projectUid))
                .and(table.orderNum.gt(orderNum));
        if (StringUtils.isBlank(storeUid))
            condition.and(table.source.eq(1));
        else
            condition.and(table.source.eq(2))
                    .and(table.storeUid.eq(storeUid));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());

        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public StoreProjectStage getLtOrderNum(String storeUid, String projectUid, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.projectUid.eq(projectUid))
                .and(table.orderNum.lt(orderNum));
        if (StringUtils.isBlank(storeUid))
            condition.and(table.source.eq(1));
        else
            condition.and(table.source.eq(2))
                    .and(table.storeUid.eq(storeUid));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.desc());

        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public boolean updateOrderNum(String storeUid, String uid, Integer orderNum, String staffUid, String staffName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid));
        if (StringUtils.isBlank(storeUid))
            condition.and(table.source.eq(1));
        else
            condition.and(table.source.eq(2))
                    .and(table.storeUid.eq(storeUid));
        return UpdateChain.of(StoreProjectStage.class)
                .set(table.orderNum, orderNum)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }


    @Override
    public StoreProjectStage getByUid(String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid));
        return getMapper().selectOneByCondition(condition);
    }

}
